﻿using gt.IdentityServer.Data;
using gt.IdentityServer.Extensions;
using gt.IdentityServer.Services;
using gt.IdentityServer.Services.Impl;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace gt.IdentityServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddDbContext<AppIdentityContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddTransient<ILoginService, LoginService>();
            services.AddAuthentication();
            /*
             * As described above, starting with v4, scopes have their own definition and can optionally be referenced by resources. 
             * Before v4, scopes where always contained within a resource.
             * To migrate to v4 you need to split up scope and resource registration, 
             * typically by first registering all your scopes (e.g. using the AddInMemoryApiScopes method), 
             * and then register the API resources (if any) afterwards. The API resources will then reference the prior registered scopes by name.
             * */

            services.AddIdentityServer(options =>
            {
                //options.UserInteraction.LogoutUrl
            })
                .AddResourceOwnerValidator<AppResourceOwnerPasswordValidator>()
                .AddProfileService<AppUserProfileService>()
                .AddSigningCredential(IdentityServerConfig.GetCertificate(Configuration))
                .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
                .AddInMemoryApiScopes(IdentityServerConfig.GetApiScopes(Configuration))
                .AddInMemoryApiResources(IdentityServerConfig.GetApiResources(Configuration))
                .AddInMemoryClients(IdentityServerConfig.GetClients(Configuration));
            //.AddInMemoryPersistedGrants()
            //.AddTestUsers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseRouting();

            app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });

            app.UseIdentityServer();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });
        }
    }
}
